/*******************************************************


This do file:

1. Takes cleaned data form the beliefs survey and produces
the final graphs.


*********************************************************/

set more off 
clear all
set segmentsize 3g

* Load Data
use $Data/Final/beliefs_cleaned, clear


local bias_extensive  1
local bias_intensive  1


if `bias_extensive' == 1{
twoway histogram be_bias_produc_ext if b1_hindu == 1, percent bins(5) color(none) lcolor(red) || ///
        histogram be_bias_produc_ext if b1_hindu == 0, percent bins(5) color(grey%10)  ///
        legend(label(1 "By Hindus") label(2 "By Muslims")) ytitle("Percent of Respondents (by religious group)") xtitle("Share ingroup names picked as more productive") xline(0.5) 
		graph export "$Output/Figures/beliefs_extensive.pdf", replace 
}		
		
if `bias_intensive' == 1{
	replace be_bias_produc_int = 4*be_bias_produc_int
	local outcome = "be_bias_produc_int" //
	
	local outtitle = "Ingroup bias in productivity levels"
	local rangemin = 0
	local rangejump = 2
	local rangemax = 8
	
	g beta = .
	g u95 = .
	g l95 = .
	g order = _n
	
	// Initialise beta as the control mean
	qui su `outcome' if b1_hindu==0
	local control_mean = r(mean)
	replace beta = r(mean) in 1
	
	// Hindu and Muslim
	reghdfe `outcome' b1_hindu, vce(robust) noabsorb
	
	replace beta = `control_mean' + _b[b1_hindu] in 4
	replace u95 = beta + _se[b1_hindu]*invttail(e(df_r),0.025) in 4
	replace l95 = beta - _se[b1_hindu]*invttail(e(df_r),0.025) in 4	
	
	** Get p-value
	test b1_hindu
	if r(p)<0.001 {
		local phindu = "{it:p}<0.001"
	}
	else if r(p)<0.01 {
		local phindu = "{it:p}<0.01"
	}
	else if r(p)>=0.995 {
		local phindu = "{it:p}=1"
	}
	else {
		local phindu = "{it:p}=0"+string(round(r(p),.01))
	}
	
	// get p-value bracket positions
	qui su u95 if order<=3
	local pbrac1Y = r(max) + 3
	local pbrac1Yl = r(max) + 1.5
	local pbrac1Yu = r(max) + 3.1
	
	local pbrac2Y = `pbrac1Y' + 5
	local pbrac2Yl = `pbrac1Yl' + 5
	local pbrac2Yu = `pbrac1Yu' + 5
	
	local pbrac3Y = `pbrac1Y' + 10
	local pbrac3Yl = `pbrac1Yl' + 10
	local pbrac3Yu = `pbrac1Yu' + 10	
	
	qui su u95 if inlist(order,1,4)
	local pbrac4Y = r(max) + 3
	local pbrac4Yl = r(max) + 1.5
	local pbrac4Yu = r(max) + 3.1
	
	// get bar mean y-position
	qui su beta
	local meanY = beta*0.5
	
	// Get means
	g betashort = string(round(beta,.1))
	forv i=1/4 {
		local mean`i' = betashort in `i'
	}	
	
	// Now Graph
	drop if order==2 | order==3
	replace order=2 if order==4
	twoway (bar beta order if order==1, barw(0.6) fc(white) lc(black) lw(medium)) ///
			(bar beta order if order==2, barw(0.6) fc(turquoise*0.3) lc(black) lw(medium)) ///	
		   (rcap u95 l95 order if order<=2, lc(black)) ///
		   (pci `pbrac4Y' 1 `pbrac4Y' 1.3 `pbrac4Yl' 1 `pbrac4Yu' 1 `pbrac4Y' 2 `pbrac4Y' 1.7 `pbrac4Yl' 2 `pbrac4Yu' 2, lc(black)), ///
		   ylabel(0(`rangejump')`rangemax', labsize(3.5) nogrid) ///
		   xlabel(, nogrid) ///
		   ysc(r(0 `rangemax')) ///
		   `title' ///
		   xlabel(1 "Muslim" 2 "Hindu", noticks nogrid labsize(3.5)) ///
		   xtick(0.5(1)2.5) ///
		   ytitle("`outtitle'", size(3.5)) ///
		   xsize(4) ///
		   xtitle("") ///	
		   text(`meanY' 1 "`mean1'", size(3)) ///
		   text(`meanY' 2 "`mean4'", size(3)) ///
		   text(`pbrac4Y' 1.5 "`phindu'", size(2.5)) ///				   
		   legend(off) 
		   graph export "$Output/Figures/beliefs_intensive.pdf", replace 

	
	
}
